//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using Shp.Runtime.Services;
using System.IO;
using System.Xml;

namespace LitwareHR.Recruiting.Workflows.Activities
{
    [ToolboxItemAttribute(typeof(ActivityToolboxItem))]
    public partial class TenantPolicyActivity : Activity
    {

        static public readonly DependencyProperty TenantIdProperty = DependencyProperty.Register("TenantId", typeof(Guid), typeof(TenantPolicyActivity), new PropertyMetadata(null));
        public TenantPolicyActivity()
        {
            InitializeComponent();
        }

        [Category("Service Parameters")]
        [Description("TenantId")]
        public Guid TenantId
        {
            get
            {
                return (Guid) base.GetValue(TenantIdProperty);
            }
            set
            {
                base.SetValue(TenantIdProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Activity targetActivity = this.GetRootWorkflow(this.Parent);

            string tmp = WorkflowLogic.GetRuleSetDef(TenantId);
            WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
            using (StringReader strReader = new StringReader(tmp))
            {
                using (XmlReader xmlReader = XmlReader.Create(strReader))
                {
                    RuleSet ruleSet = serializer.Deserialize(xmlReader) as RuleSet;
                    if (ruleSet != null)
                    {
                        RuleValidation validation = new RuleValidation(targetActivity.GetType(), null);
                        RuleExecution execution = new RuleExecution(validation, targetActivity, executionContext);
                        ruleSet.Execute(execution);
                    }
                    return base.Execute(executionContext);
                }
            }
        }

        private CompositeActivity GetRootWorkflow(CompositeActivity activity)
        {
            if (activity.Parent != null)
            {
                CompositeActivity workflow = GetRootWorkflow(activity.Parent);
                return workflow;
            }
            else
            {
                return activity;
            }
        }
    }
}
